﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ECare.Client.templates;
using ECare.Client;
using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmCheck : frmGridTemplate
    {
        public frmCheck()
        {
            InitializeComponent();

            this.Load += frmCheck_Load;

            this.grd.CellDoubleClick += grd_CellDoubleClick;
            this.grd.CellFormatting += grd_CellFormatting;
            this.grd.DataError += grd_DataError;
        }

        void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           if(e.RowIndex==-1)
               return;
            

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as SupplyInfo;
            var frm = new frmInInfoReadonly { CurrentSupply = entitye };
            frm.ShowDialog();
        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                if (grd.Columns[e.ColumnIndex].Name == "supply_checkstatus")
                {
                    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    if (currentstatus == CheckStatus.Qualitified.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (currentstatus == CheckStatus.Rejected.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (currentstatus == CheckStatus.UnFinished.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {

                    }

                }

                //if (grd.Columns[e.ColumnIndex].Name == "HasStorage")
                //{

                //    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().ToLower();
                //    if (currentstatus == "true")
                //        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                //}

            }

        }
        public BindingList<SupplyInfo> Source { get; set; }
        void frmCheck_Load(object sender, EventArgs e)
        {
            resource_id_column.DataSource = ResourcesHelper.GetAllResources();
            resource_id_column.DisplayMember = "ResourceName";
            resource_id_column.ValueMember = "ResourceId";


            supply_create_by.DataSource = UserInfoHelper.GetUsers();
            supply_create_by.DisplayMember = "UserName";
            supply_create_by.ValueMember = "UserId";


            supply_check_by.DataSource = UserInfoHelper.GetUsers();
            supply_check_by.DisplayMember = "UserName";
            supply_check_by.ValueMember = "UserId";



         //   this.Source = new BindingList<SupplyInfo>(SupplyInfoHelper.GetAllSupplyInfo());
        //    this.grd.DataSource = this.Source;
            this.LoadData("1=1");

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;

            if (entitye.HasStorage)
            {
                MessageBox.Show("已经正式入库，不能检验.");
                return;
            }


            var frm = new frmCheckInfo();
            frm.entity = entitye;
            //   frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;
            if (entitye.supply_checkstatus != CheckStatus.Qualitified)
            {
                MessageBox.Show("当前入库单未检验完成，检验完成后方可入库.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var frm = new frmCreateStorage();
            frm.entity = entitye;
            frm.ShowDialog();
            

        }
        private void LoadData(string where){
            this.Source = new BindingList<SupplyInfo>(SupplyInfoHelper.GetAllSupplyInfo(where));
            this.grd.DataSource = new BindingSource(this.Source, null);
        }

        private void eButton1_Click(object sender, EventArgs e)
        {
            var frm = new frmSupplyQuery();
            frm.SearchCompleted += frm_SearchCompleted;
            frm.ShowDialog();
        }
        void frm_SearchCompleted(string where)
        {
            //throw new NotImplementedException();

            this.LoadData(where);
        }
    }
}
